<script lang="ts" setup>
const router = useRouter()

const route = useRoute()

const { themeVars, theme } = useManualTheme()

const { activeTabbar, getTabbarItemValue, setTabbarItemActive, tabbarList } = useTabbar()

function handleTabbarChange({ value }: { value: string }) {
  console.log('Tabbar clicked:', value)
  setTabbarItemActive(value)
  router.pushTab({ name: value })

}

onMounted(() => {

  uni.hideTabBar()

  nextTick(() => {
    // 根据当前路由设置active tabbar
    const currentRoute = route.path
    let activeTabName = 'index' // 默认首页
    
    if (currentRoute && currentRoute.includes('/pages/category/')) {
      activeTabName = 'category'
    } else if (currentRoute && currentRoute.includes('/pages/cart/')) {
      activeTabName = 'cart'
    } else if (currentRoute && currentRoute.includes('/pages/user/')) {
      activeTabName = 'user'
    } else if (currentRoute && currentRoute.includes('/pages/index/')) {
      activeTabName = 'index'
    }
    
    setTabbarItemActive(activeTabName)
  })
})
</script>

<script lang="ts">
export default {
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: 'shared',
  },
}
</script>

<template>
  <wd-config-provider :theme-vars="themeVars" :custom-class="`page-wraper ${theme}`" :theme="theme">
    <slot />
    <wd-tabbar
      :model-value="activeTabbar.name" bordered safe-area-inset-bottom placeholder fixed
      @change="handleTabbarChange"

    >
      <wd-tabbar-item
        v-for="(item, index) in tabbarList" :key="index" :name="item.name"
        :value="item.name" :title="item.title" :icon="item.icon"
        :badge="item.value"
      
      />
    </wd-tabbar>
    <!-- #ifdef MP-WEIXIN -->
    <privacy-popup />
    <!-- #endif -->
    <wd-notify />
    <wd-message-box />
    <wd-toast />
    <global-loading />
    <global-toast />
    <global-message />
  </wd-config-provider>
</template>

<style lang="scss">
.page-wraper {
  min-height: calc(100vh - var(--window-top));
  box-sizing: border-box;
  background: #f9f9f9;
}

.wot-theme-dark.page-wraper {
  background: #222;
}
</style>
